<?php

namespace XWAM\Controller;

/**
 * 用户控制器（包含管理员和座席）
 *
 * @since 1.0 <2014-6-13> sutroon Added.
 */
class Admin extends AppbaseController {

    function indexSearch() {
        $search = I('param.');        
        if (!empty($search['word'])) {
            $word = $search['word'];
            $where['_string'] = "user_name like '%$word%' or name like '%$word%' or mobile like '%$word%'";
        }
        if (!empty($search['department_id'])) {
            $where['department_id'] = $search['department_id'];
        }
        $search['type_name'] = 'ADMIN';
        $where['type_name'] = $search['type_name'];
        // $where['site_id'] = $this->site_id;
        return array('search' => $search, 'where' => $where);
    }

    public function index() {
        $asearch = $this->indexSearch();
        $this->assign('usertype_list', array('ADMIN' => '管理员', 'SERVICES' => '客服人员', 'CUSTOMER' => 'VIP客户'));
        $mAdmin = new \XWAM\Model\AdminModel();
        $list = $mAdmin->selectPaging($page, array('where' => $asearch['where'], 'order' => 'id'), array('page_params' => $asearch['search']));
        $list= \XWAM\Model\UserModel::convertTextAll($list);
        $this->assign('search', $asearch['search']);
        $this->assign('list', $list);
        $this->assign('page', $page->showBootstrap());
        $this->display();
    }

    public function export() {
        $mAdmin = new \XWAM\Model\AdminModel();
        $list = $mAdmin->select();
        if (!$list) {
            $this->error('找不到符合条件的记录!');
        }
        sofn_excel_export($list);
    }

    /**
     * 编辑个人资料
     * @since 1.0 <2015-11-11> SoChishun Added.
     */
    function profile() {
        $mAdmin = new \XWAM\Model\AdminModel();
        $data=$mAdmin->find($this->loginData['id']);
        $this->assign('data',$data);
        $this->display();
    }

    /**
     * 保存个人资料
     * @since 1.0 <2016-3-11> SoChishun Added.
     */
    function profileSave() {
        $mAdmin = new \XWAM\Model\AdminModel();
        $result = $mAdmin->saveUser($this->loginData['id']);
        $this->dialogClose($result['status'], $result['info']);
    }

    public function edit($id = 0) {
        $mAdmin = new \XWAM\Model\AdminModel();
        $data = $id ? $mAdmin->find($id) : array();
        $this->assign('data', $data);
        $this->assign('meta_title', $id ? '编辑 ' . $data['user_name'] : '新增管理员');
        $this->display();
    }

    public function editSave() {
        $mAdmin = new \XWAM\Model\AdminModel();
        $result = $mAdmin->saveUser($this->loginData['id']);
        $this->dialogJump($result['status'], $result['info']);
    }

    public function delete($id = '') {
        if (!$id) {
            $this->ajaxMsg(false, '编号丢失!');
        }
        if ($id == $this->loginData['id']) {
            $this->ajaxMsg(false, '不能删除自己!');
        }
        $mAdmin = new \XWAM\Model\AdminModel();
        $result = $mAdmin->delete_user($id, $this->loginData['id']);
        $this->ajaxMsg($result, '删除{%}!');
    }

}
